
package com.loong.loong.hik.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.loong.loong.common.core.util.R;
import com.loong.loong.hik.entity.Car;
import com.loong.loong.hik.mapper.CarMapper;
import com.loong.loong.hik.property.request.car.CarBatchAddRequestDto;
import com.loong.loong.hik.property.request.car.CarbatchDeleteRequestDto;
import com.loong.loong.hik.property.request.resource.ResourcePersonConditionPersoninfoRequest;
import com.loong.loong.hik.property.result.car.CarBatchAddResultVo;
import com.loong.loong.hik.property.result.car.CarSingleUpdateResultVo;
import com.loong.loong.hik.property.result.car.CarbatchDeleteResultVo;
import com.loong.loong.hik.property.result.resource.ResourcePersonConditionPersoninfoResult;
import com.loong.loong.hik.property.urls.face.HikFaceUrl;
import com.loong.loong.hik.service.CarService;
import com.loong.loong.hik.service.FaceService;
import com.loong.loong.hik.service.HikCarVService;
import com.loong.loong.hik.service.HikSourceVService;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 车辆
 *
 * @author jinyiang
 * @date 2023-03-30 10:02:49
 */
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car> implements CarService {

	@Resource
	private HikCarVService hikCarVService;
	@Resource
	private FaceService faceService;
	@Resource
	private HikSourceVService hikSourceVService;

	@Override
	public R add(Car car) {
		if (HikFaceUrl.SWITCH.equals("Y")){
			try {
			CarBatchAddRequestDto carBatchAddRequestDto = new CarBatchAddRequestDto();
			carBatchAddRequestDto.setClientId(HikFaceUrl.clientId);
			carBatchAddRequestDto.setDescription(car.getDescription());
			ResourcePersonConditionPersoninfoRequest resourcePersonConditionPersoninfoRequest = new ResourcePersonConditionPersoninfoRequest();
			resourcePersonConditionPersoninfoRequest.setParamName("phoneNo");
			List<String> list = new ArrayList<>();
			list.add(car.getLinkPhone());
			resourcePersonConditionPersoninfoRequest.setParamValue(list);
				ResourcePersonConditionPersoninfoResult resourcePersonConditionPersoninfoResult =
						hikSourceVService.personConditionPersoninfo(resourcePersonConditionPersoninfoRequest);
				if (resourcePersonConditionPersoninfoResult!=null&&"0".equals(resourcePersonConditionPersoninfoResult.getCode())
				&&resourcePersonConditionPersoninfoResult.getData()!=null&&resourcePersonConditionPersoninfoResult.getData().getList()!=null
				&&resourcePersonConditionPersoninfoResult.getData().getList().size()>0){
					List<ResourcePersonConditionPersoninfoResult.Data.Person> list1 = resourcePersonConditionPersoninfoResult.getData().getList();
					carBatchAddRequestDto.setPersonId(list1.get(0).getPersonId());
					car.setPersonId(list1.get(0).getPersonId());
				}
				carBatchAddRequestDto.setPlateNo(car.getCarNo());
				carBatchAddRequestDto.setDescription(car.getDescription());
				carBatchAddRequestDto.setPlateType(car.getPlateType());
				carBatchAddRequestDto.setPlateColor(car.getPlateColor());
				carBatchAddRequestDto.setVehicleColor(car.getVehicleColor());
				carBatchAddRequestDto.setVehicleType(car.getVehicleType());
				CarBatchAddResultVo carBatchAddResultVo = hikCarVService.batchAdd(carBatchAddRequestDto);
				if (carBatchAddResultVo!=null){
					if ("0".equals(carBatchAddResultVo.getCode())){
						if (carBatchAddResultVo.getData()!=null&&carBatchAddResultVo.getData().getSuccesses()!=null
						&&carBatchAddResultVo.getData().getSuccesses().size()>0){
							List<CarBatchAddResultVo.Success> successes = carBatchAddResultVo.getData().getSuccesses();
							car.setSysNo(successes.get(0).getVehicleId());
						}
					}else{
						return R.failed(carBatchAddResultVo.getMsg());

					}
				}else{
					return R.failed("接口返回为空");
				}

			} catch (Exception e) {
				e.printStackTrace();
				return R.failed(e.getMessage());
			}
		}
		return add(car);
	}

	@Override
	public R update(Car car) {
		if (HikFaceUrl.SWITCH.equals("Y")){
			try {
				CarBatchAddRequestDto carBatchAddRequestDto = new CarBatchAddRequestDto();
				carBatchAddRequestDto.setClientId(HikFaceUrl.clientId);
				carBatchAddRequestDto.setDescription(car.getDescription());
				ResourcePersonConditionPersoninfoRequest resourcePersonConditionPersoninfoRequest = new ResourcePersonConditionPersoninfoRequest();
				resourcePersonConditionPersoninfoRequest.setParamName("phoneNo");
				List<String> list = new ArrayList<>();
				list.add(car.getLinkPhone());
				resourcePersonConditionPersoninfoRequest.setParamValue(list);
				ResourcePersonConditionPersoninfoResult resourcePersonConditionPersoninfoResult =
						hikSourceVService.personConditionPersoninfo(resourcePersonConditionPersoninfoRequest);
				if (resourcePersonConditionPersoninfoResult!=null&&"0".equals(resourcePersonConditionPersoninfoResult.getCode())
						&&resourcePersonConditionPersoninfoResult.getData()!=null&&resourcePersonConditionPersoninfoResult.getData().getList()!=null
						&&resourcePersonConditionPersoninfoResult.getData().getList().size()>0){
					List<ResourcePersonConditionPersoninfoResult.Data.Person> list1 = resourcePersonConditionPersoninfoResult.getData().getList();
					carBatchAddRequestDto.setPersonId(list1.get(0).getPersonId());
				}
				carBatchAddRequestDto.setPlateNo(car.getCarNo());
				carBatchAddRequestDto.setDescription(car.getDescription());
				carBatchAddRequestDto.setPlateType(car.getPlateType());
				carBatchAddRequestDto.setPlateColor(car.getPlateColor());
				carBatchAddRequestDto.setVehicleColor(car.getVehicleColor());
				carBatchAddRequestDto.setVehicleType(car.getVehicleType());
				CarSingleUpdateResultVo carBatchAddResultVo = hikCarVService.singleUpdate(carBatchAddRequestDto);
				if (carBatchAddResultVo!=null){
					if (!"0".equals(carBatchAddResultVo.getCode())){
						return R.failed(carBatchAddResultVo.getMessage());
					}
				}else{
					return R.failed("接口返回为空");
				}

			} catch (Exception e) {
				e.printStackTrace();
				return R.failed(e.getMessage());
			}
		}
		return update(car);
	}

	@Override
	public R remove(Integer id) {
		if ("Y".equals(HikFaceUrl.SWITCH)){
			Car car = getById(id);
			if (car==null){
				return R.failed("根据id查询不存在");
			}
			CarbatchDeleteRequestDto carbatchDeleteRequestDto = new CarbatchDeleteRequestDto();
			List<String> list = new ArrayList<>();
			list.add(car.getSysNo());
			carbatchDeleteRequestDto.setVehicleIds(list);
			try {
				CarbatchDeleteResultVo carbatchDeleteResultVo = hikCarVService.batchDelete(carbatchDeleteRequestDto);
				if (carbatchDeleteResultVo!=null){
					if (!"0".equals(carbatchDeleteResultVo.getCode())){
						return R.failed(carbatchDeleteResultVo.getMessage());
					}
				}else {
					return R.failed("接口返回值为空");
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return remove(id);
	}
}
